//
//  HDMapLargeImgVC.m
//  HDWuDaDaoServiceCenter
//
//  Created by HDNiuKuiming on 2017/4/10.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HDMapLargeImgVC.h"
#import "HDPlayerView.h"
#import "HDSceneObj.h"
#import "HDMapObj.h"

@interface HDMapLargeImgVC ()
@property (nonatomic, strong) HDPlayerView *playerView;
@property (weak, nonatomic) IBOutlet UIView *playerBgView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) HDSceneObj *nowSceneObj;
@property (nonatomic, assign) CGFloat *divisionNumber;
@property (nonatomic, strong) UIImageView *sceneImgView;
@property (nonatomic, strong) NSMutableArray *poiArray;

@end

@implementation HDMapLargeImgVC

#pragma mark
#pragma mark -------- Init Methods --------

- (void)viewDidLoad {
    [super viewDidLoad];
    [self playViewInit];
    _poiArray = [NSMutableArray new];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playToEndNotification) name:kPlayer_Play_To_End_Notification object:nil];

    [self requestSceneAction];
    _playerView.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_playerView pauseAudioAction];
}

#pragma mark
#pragma mark -------- Init Methods --------

- (void)playViewInit {
    _playerView = [[[NSBundle mainBundle] loadNibNamed:@"HDPlayerView" owner:self options:nil] lastObject];
    [_playerBgView addSubview:_playerView];
    [_playerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_playerBgView).with.offset(0);
        make.right.equalTo(_playerBgView).with.offset(0);
        make.left.equalTo(_playerBgView).with.offset(0);
        make.bottom.equalTo(_playerBgView).with.offset(0);
    }];
}

- (void)requestSceneAction
{
    NSString *sceneID = _sceneObj.exhibit_id;
    if (sceneID.length == 0)
        return;
    
    HDRequestObj *loginObj = [[HDRequestObj alloc] init];
    NSString *urlPath = [NSString stringWithFormat:@"%@g=mapi&m=Contentinfo&a=scene_detail_info", HTTP_Request_URL_Prefix];
    NSDictionary *parameters = @{@"language": @"CHINESE",
                                 @"scene_id":sceneID};
    
    [loginObj requestGetPath:urlPath Parameter:parameters withVC:self timeout:5];
    loginObj.myDataBlock = ^(NSDictionary *dataDic){
        
        NSString *loginCode = dataDic[@"status"];
        if (loginCode.integerValue == 1) {
            
            NSDictionary *objDic = dataDic[@"data"];
            NSDictionary *scene_infoDic = objDic[@"scene_info"];
            _nowSceneObj = [HDSceneObj sceneObjWithDict:scene_infoDic];
            
            [_poiArray removeAllObjects];
            NSArray *exihibitArr = objDic[@"exhibit_info"];
            for (NSInteger i=0;i<exihibitArr.count;i++) {
                NSDictionary *dicTemp = exihibitArr[i];
                HDMapObj *obj = [HDMapObj mapPoiWithDict:dicTemp];
                [_poiArray addObject:obj];
            }
            
            [self reloadScrollView];
        }
        else {
        }
    };
}

- (void)reloadScrollView {
    _sceneImgView = [UIImageView new];
    [_sceneImgView sd_setImageWithURL:[NSURL URLWithString:_nowSceneObj.img] placeholderImage:[UIImage imageNamed:@"mapview_bg"]];
    
    CGFloat hScale = _nowSceneObj.height.floatValue/_scrollView.frame.size.height;
    CGSize imgSize = CGSizeMake(_nowSceneObj.width.floatValue/hScale, _nowSceneObj.height.floatValue/hScale);
    if (_nowSceneObj.width.floatValue == 0 || _nowSceneObj.height.floatValue <= 0)
        return;
    
    CGFloat width = imgSize.width;
    _scrollView.contentSize = imgSize;
    _sceneImgView.frame = CGRectMake(0, 0, width, imgSize.height);
    _sceneImgView.contentMode = UIViewContentModeScaleAspectFit;
    
    [_scrollView addSubview:_sceneImgView];
    [_scrollView addSubview:_sceneImgView];
    
    for (HDMapObj *obj in _poiArray) {
        CGFloat x = obj.axis_x.floatValue/hScale;
        CGFloat y = obj.axis_y.floatValue/hScale;
        
        [self showAnimateBtn:CGPointMake(x, y) withTag:obj.exhibit_id.integerValue];
    }
}

- (void)showAnimateBtn:(CGPoint)point
               withTag:(NSInteger)tagInt
{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(point.x, point.y-40, 50, 50)];
    [btn addTarget:self action:@selector(playAudioAction:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = tagInt;
    UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(point.x, point.y-40, 50, 50)];
    NSArray *imgArray = @[[UIImage imageNamed:@"map_animate_1"],
                          [UIImage imageNamed:@"map_animate_2"],
                          [UIImage imageNamed:@"map_animate_3"],
                          [UIImage imageNamed:@"map_animate_4"]];
    [bgImgView setAnimationImages:imgArray];
    [bgImgView setAnimationDuration:2.0f];
    [bgImgView startAnimating];
    
    [_scrollView addSubview:bgImgView];
    [_scrollView addSubview:btn];
}

- (void)playAudioAction:(UIButton *)btn {

    HDMapObj *obj = nil;
    for (HDMapObj *tempObj in _poiArray) {
        if (tempObj.exhibit_id.integerValue == btn.tag) {
            obj = tempObj;
            break;
        }
    }
    
    if (obj != nil) {
        [_playerView playItem:obj];
        _playerView.nameLabel.text = obj.title;
        [_playerView.leftImgView sd_setImageWithURL:[NSURL URLWithString:obj.route_img_path] placeholderImage:[UIImage imageNamed:@"player_default"]];
        
        if (obj.type == nil && obj.route_img_path == nil && [obj.img isKindOfClass:[NSString class]]) {
            NSString *imgStr = (NSString *)obj.img;
            [_playerView.leftImgView sd_setImageWithURL:[NSURL URLWithString:imgStr] placeholderImage:[UIImage imageNamed:@"player_default"]];
        }
        
        [_playerBgView setHidden:NO];
        [_playerView setHidden:NO];
    }
}

- (void)playToEndNotification {
    if ([self.navigationController.visibleViewController isKindOfClass:[self class]])
        _playerBgView.hidden = YES;
}

#pragma mark
#pragma mark -------- Dealloc Methods --------

- (IBAction)backAction:(id)sender {
    [super back];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
